package com.cat.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cat.yygh.cmn.listener.DictListener;
import com.cat.yygh.cmn.mapper.DictMapper;
import com.cat.yygh.cmn.service.DictService;
import com.cat.yygh.model.cmn.Dict;
import com.cat.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 医院设置表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-11-03
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public List<Dict> getChildListByPid(Long pid) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("parent_id",pid);
        List<Dict> items = baseMapper.selectList(dictQueryWrapper);
        for (Dict item : items) {
            item.setHasChildren(isHasChildren(item.getId()));
        }
        return items;
    }

    @Override
    public void upload(MultipartFile multipartFile) throws IOException {
        EasyExcel.read(multipartFile.getInputStream(),
                DictEeVo.class,new DictListener(baseMapper)).sheet(0).doRead();
    }

    @Override
    public void download(HttpServletResponse response) throws IOException {
        /*查询数据*/
        List<Dict> list = baseMapper.selectList(null);
        ArrayList<DictEeVo> dictEeVoList = new ArrayList<>(list.size());
        for (Dict dict : list) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);
        }
        /*设置下载请求头*/
        response.setContentType("application/vnd.ms.excel");
        response.setCharacterEncoding("utf-8");
        String filename = URLEncoder.encode("字典文件","utf-8");
        response.setHeader("Content-disposition","attachment;filename="+filename+".xlsx");

        EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("学生列表1").doWrite(dictEeVoList);
    }

    @Override
    public String getNameByValue(Long value) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("value",value);
        Dict dict = baseMapper.selectOne(dictQueryWrapper);
        if (dict != null){
            return dict.getName();
        }
        return null;
    }
    @Override
    public String getNameByDictCodeAndValue(String dictCode, Long value) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("dict_code",dictCode);
        Dict dict = baseMapper.selectOne(dictQueryWrapper);

        QueryWrapper<Dict> dictQueryWrapper1 = new QueryWrapper<>();
        dictQueryWrapper1.eq("parent_id", dict.getId());
        dictQueryWrapper1.eq("value", value);
        Dict dict1 = baseMapper.selectOne(dictQueryWrapper1);
        return dict1.getName();
    }

    @Override
    public List<Dict> findByDictCode(String dictCode) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("dict_code",dictCode);
        return baseMapper.selectList(dictQueryWrapper);
    }

    private boolean isHasChildren(Long id) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("parent_id",id);
        Integer i = baseMapper.selectCount(dictQueryWrapper);
        return i>0;
    }
}

